package cn.hhit.ximalaya;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

//import android.app.FragmentManager;
import android.graphics.Color;
import android.os.Bundle;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import cn.hhit.ximalaya.adapters.IndicatorAdapter;
import cn.hhit.ximalaya.adapters.MainContent_ViewPager_Adapter;
import cn.hhit.ximalaya.utils.LogUtil;

public class MainActivity extends AppCompatActivity{

    private IndicatorAdapter indicatorAdapter;
    private ViewPager mainViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }


    public void initView(){
        //指示器
        MagicIndicator magicIndicator = findViewById(R.id.main_indicator);

        //设置背景颜色
        magicIndicator.setBackgroundColor(Color.parseColor("#F97254"));

        //适配器
        indicatorAdapter = new IndicatorAdapter(this);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(indicatorAdapter);
        commonNavigator.setAdjustMode(true);
        magicIndicator.setNavigator(commonNavigator);



        //创造一个fragmentManager
        FragmentManager fragmentManager=getSupportFragmentManager();

        //把viewPager和indicator
        mainViewPager = findViewById(R.id.main_pager);
        //viewPager设置适配器
        mainViewPager.setAdapter(new MainContent_ViewPager_Adapter(fragmentManager));

        ViewPagerHelper.bind(magicIndicator, mainViewPager);









    }

    private void initEvent() {
        indicatorAdapter.setOnIndicatorTapClickListener(new IndicatorAdapter.OnIndicatorTapClickListener() {
            @Override
            public void onTabClick(int index) {
                 LogUtil.e("mainActivity", "click index is -- > " + index);
                if (  mainViewPager != null) {
                    mainViewPager.setCurrentItem(index, false);
                }
            }
        });
    }
}